/*
 * Copyright (C) 2011 Fabio Hufschmid at <hufsf1@bfh.ch>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package ch.bfh.aeneas.core.io.tts.bloomfilter;


import com.skjegstad.utils.BloomFilter;
import android.content.Context;
import android.util.Log;
import java.io.IOException;
import java.util.BitSet;
import java.util.Properties;

/**
 * The LangFinder calss is responsible to load the needed binary files for the bloomfilter from the sd-card
 * @author Fabio Hufschmid at <hufsf1@bfh.ch>
 */
public class LangFinder {
    private String TAG = "LangFinder";
    
    private BloomFilter<String> mDeBf;
    private BloomFilter<String> mFrBf; 
    private BloomFilter<String> mEnBf;
    private Properties mDeProp=null;
    private int deElements;
    private double deAccuracy;
    private Properties mEnProp=null;
    private int enElements;
    private double enAccuracy;
    private Properties mFrProp=null;
    private int frElements;
    private double frAccuracy;
    
    /**This class is needed to define the language of a word
     * Bloomfilter initalisation and import of the BitSet from precalculated Files
     * 
     * @param ctx
     * @throws IOException 
     */
    public LangFinder(Context ctx) throws IOException
    {
        UtilsBloomfilter ub = new UtilsBloomfilter();
        
        mDeProp=ub.readIniFile("/mnt/sdcard/aeneas/de_bloomfilter.dat.ini");
        mEnProp=ub.readIniFile("/mnt/sdcard/aeneas/en_bloomfilter.dat.ini");
        mFrProp=ub.readIniFile("/mnt/sdcard/aeneas/fr_bloomfilter.dat.ini");

            if(mDeProp != null)
            {
                deElements = Integer.parseInt(mDeProp.getProperty("elements"));
                deAccuracy = Double.parseDouble(mDeProp.getProperty("accuracy"));
            }
            else
            {
                deElements=1000;
                deAccuracy=0.001;
            }
            if(mEnProp != null)
            {
                enElements = Integer.parseInt(mEnProp.getProperty("elements"));
                enAccuracy = Double.parseDouble(mEnProp.getProperty("accuracy"));
            }
            else
            {
                enElements=1000;
                enAccuracy=0.001;
            }
            if(mFrProp != null)
            {
                frElements = Integer.parseInt(mFrProp.getProperty("elements"));
                frAccuracy = Double.parseDouble(mFrProp.getProperty("accuracy"));
            }
            else
            {
                frElements=1000;
                frAccuracy=0.001;
            }
     
        mDeBf =new BloomFilter<String>(deAccuracy, deElements);
        mFrBf =new BloomFilter<String>(frAccuracy, frElements);
        mEnBf =new BloomFilter<String>(enAccuracy, enElements);
    
        mDeBf.setBitSet(ub.importBitSetToBloomfilter("/mnt/sdcard/aeneas/de_bloomfilter.dat"));
        mEnBf.setBitSet(ub.importBitSetToBloomfilter("/mnt/sdcard/aeneas/en_bloomfilter.dat"));
        mFrBf.setBitSet(ub.importBitSetToBloomfilter("/mnt/sdcard/aeneas/fr_bloomfilter.dat"));

    }
    
    /**this mehtode finds and returns the languageshortcut of a word, if the word is containing in the bloomfilter 
     * 
     * @param inStr the word to define the languageshortcut
     * @return      the languageshortcut (de,fr,en)
     */
     public String getLang(String inStr)
        {
            String out="notFound";
            Log.i(TAG, "Zu testender String: "+inStr);
            if(mDeBf.contains(inStr))
            {
                out="de";
                Log.i(TAG, "String ist deutsch");
            }
            if(mFrBf.contains(inStr))
            {
                out="fr";
                Log.i(TAG, "String ist franz");
            }
            if(mEnBf.contains(inStr))
            {
                out="en";
                Log.i(TAG, "String ist englisch");
            }
            Log.i(TAG, "Test String return: "+out);
            return out;
        }
    
    
    
}
